using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace GraphAlgsPresentation.Core
{
    /// <summary>
    /// Represents a user control of CommentControl
    /// </summary>
    [Serializable]
    public partial class CommentControl : UserControl
    {
        public List<CommentLine> Lines;
        private RichTextBox rtBoxTemp;

        #region Initialze

        /// <summary>
        /// Initializes a new instance of the <see cref="CommentControl"/> class.
        /// </summary>
        public CommentControl()
        {
            InitializeComponent();
            Lines = new List<CommentLine>();
            rtBoxTemp = new RichTextBox();
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Inserts the specified text.
        /// </summary>
        /// <param name="text">The text.</param>
        public void Insert(string text)
        {
            CommentLine line = new CommentLine();
            line.Text = text;
            Lines.Add(line);
            UpdateComment();
        }

        /// <summary>
        /// Inserts the specified text at the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="text">The text.</param>
        public void Insert(int index, string text)
        {
            CommentLine line = new CommentLine();
            line.Text = text;
            Lines.Insert(index, line);
            UpdateComment();
        }

        /// <summary>
        /// Sets the color of the background at the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="color">The color.</param>
        public void SetBackColor(int index, Color color)
        {
            Lines[index].BackColor = color;
            UpdateComment();
        }

        /// <summary>
        /// Sets the color of the text at the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="color">The color.</param>
        public void SetTextColor(int index, Color color)
        {
            Lines[index].TextColor = color;
            UpdateComment();
        }

        /// <summary>
        /// Sets the font of the text at the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="font">The font.</param>
        public void SetFont(int index, Font font)
        {
            Lines[index].Font = font;
            UpdateComment();
        }

        /// <summary>
        /// Sets the text at the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="text">The text.</param>
        public void SetText(int index, string text)
        {
            if (index > 50) return;
            while (index >= Lines.Count) 
                Lines.Add(new CommentLine());
            Lines[index].Text = text;
            UpdateComment();
        }

        /// <summary>
        /// Resets the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        public void ResetLine(int index)
        {
            Lines[index].BackColor = CommentLine.DefaultBackColor;
            Lines[index].TextColor = CommentLine.DefaultTextColor;
            Lines[index].Font = CommentLine.DefaultFont;
            UpdateComment();
        }

        /// <summary>
        /// Highlights the specified line.
        /// </summary>
        /// <param name="index">The index.</param>
        public void HighlightLine(int index)
        {
            Lines[index].BackColor = Color.Blue;
            Lines[index].TextColor = Color.White;
            UpdateComment();
        }

        /// <summary>
        /// Clears all comments.
        /// </summary>
        public void Clear()
        {
            Lines.Clear();
            UpdateComment();
        }

        #endregion

        /// <summary>
        /// Updates the comment's content.
        /// </summary>
        private void UpdateComment()
        {
            rtBoxTemp.Clear();
            foreach (CommentLine line in Lines)
            {
                string text = line.Text;
                while (text.Length > 0 && text[0] == '\t')
                {
                    rtBoxTemp.AppendText("\t");
                    text = text.Substring(1);
                }
                rtBoxTemp.SelectionBackColor = line.BackColor;
                rtBoxTemp.SelectionColor = line.TextColor;
                rtBoxTemp.SelectionFont = line.Font;
                rtBoxTemp.AppendText(text + Environment.NewLine);
            }
            rtbox.Rtf = rtBoxTemp.Rtf;
        }
    }
}
